﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;
public partial class Bidder_CompanyConstractLookDetail : System.Web.UI.Page
{
    string _strProjectIdLookDeltail;
    public string _AreaIdLookDeltail = "";
    string _strHopitalIdLookDeltail, _strTbridLookDeltail, _strPsLookDeltail, _strScLookDeltail;
    protected void Page_Load(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            _AreaIdLookDeltail = SysFun.ToTrim(_Account.AreaId);
            _strHopitalIdLookDeltail = SysFun.IsSQL(Request.QueryString["AccountId_YY"]);
            _strTbridLookDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_TB"]));
            _strPsLookDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_PS"]));
            _strScLookDeltail = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["AccountId_SC"]));
            _strProjectIdLookDeltail = SysFun.IsSQL(Request.QueryString["ProjectId"]);
            showGridView();

        }

    }


    public void showGridView()
    {
        string priceTitle = "挂网价";
        string fieldName = "";
        string field = "";
        switch (_strProjectIdLookDeltail)
        {
            case "8":
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice) as " + fieldName;
                break;
            case "9":
                priceTitle = "最高零售价";
                break;
            case "10":
                priceTitle = "最高限价";
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice) as " + fieldName;
                break;
            case "11":
                break;
            case "12":
                priceTitle = "最高零售价";
                break;

        }
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);

        dbPage.Table = "PurchaseTable";
        dbPage.SelectField = " HspId" + field + ",CompanyName_TB,GoodsId,ProductName,GoodsName,MedicineModel,Outlookc,Factor,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,PurchasePrice,accountid_sc,UserCode_SC,CompanyName_SC,CompanyName_TB,UserCode_TB,HospitalName,accountid_ps,UserCode_PS,CompanyName_PS,BidPrice,PlanPurNum,convert(numeric(30,2),BidPrice*PlanPurNum) as PlanPrice";//要显示的字段
        dbPage.Key = "HspId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);         //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HspId,CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='left' cellpadding='2' cellspacing='1'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td >药品流水号</td>");
        strHtml.Append("<td >通用名</td>");
        strHtml.Append("<td >剂型</td>");
        strHtml.Append("<td >规格</td>");
        strHtml.Append("<td >转换系数</td>");
        strHtml.Append("<td >包装规格</td>");
        strHtml.Append("<td >质量层次</td>");

        strHtml.Append("<td>" + priceTitle + "</td>");
        if (field != "")
        {
            strHtml.Append("<td>" + fieldName + "</td>");
        }
        strHtml.Append("<td >生产企业注册号</td>");
        strHtml.Append("<td >生产企业</td>");
        strHtml.Append("<td >配送企业注册号</td>");
        strHtml.Append("<td >配送企业</td>");
        strHtml.Append("<td >中标人注册号</td>");
        strHtml.Append("<td >中标人</td>");
        strHtml.Append("<td >医院名称</td>");
        strHtml.Append("<td >合同价</td>");
        strHtml.Append("<td >合同数量</td>");
        strHtml.Append("<td >合同金额</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
            strHtml.Append("<td> " + dr["GoodsId"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["ProductName"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["MedicineModel"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["Outlookc"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["Factor"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["Pack1"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["QualityLevels"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["BidPrice"].ToString() + " </td>");
            if (field != "")
            {
                strHtml.Append("<td  >" + dr[fieldName].ToString() + "</td>");
            }

            strHtml.Append("<td> " + dr["UserCode_SC"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["CompanyName_SC"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["UserCode_PS"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["CompanyName_PS"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["UserCode_TB"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["CompanyName_TB"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["HospitalName"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["PurchasePrice"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["PlanPurNum"].ToString() + " </td>");
            strHtml.Append("<td> " + dr["PlanPrice"].ToString() + " </td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }


    public string fctConditions()
    {

        string _strTemp = " State='正常采购' and ProjectId='" + _strProjectIdLookDeltail + "' And ";
        //投标人名称
        string _strCompanyName_TB = this.txtCompanyName_TB.Text.ToString();
        //投标人ID
        if (_strTbridLookDeltail != "")
        {
            _strTemp += " AccountId_TB=" + _strTbridLookDeltail + " And";
        }
        if (_strCompanyName_TB != "")
        {
            _strTemp += " CompanyName_TB like '%" + _strCompanyName_TB + "%' and";
        }
        //投标人编码
        string _strUserCode_TB = this.txtUserCode_TB.Text.ToString();
        if (_strUserCode_TB != "")
        {
            _strTemp += " UserCode_TB like '%" + _strUserCode_TB + "%' and";
        }
        //配送企业ID
        if (_strPsLookDeltail != "")
        {
            _strTemp += " AccountId_PS=" + _strPsLookDeltail + " And";
        }
        //配送企业名称
        string _strCompanyName_PS = this.txtCompanyName_PS.Text.ToString();
        if (_strCompanyName_PS != "")
        {
            _strTemp += " CompanyName_PS like '%" + _strCompanyName_PS + "%' and";
        }

        //配送企业编码
        string _strUserCode_PS = this.txtUserCode_PS.Text.ToString();
        if (_strUserCode_PS != "")
        {
            _strTemp += " UserCode_PS like '%" + _strUserCode_PS + "%' and";
        }
        //医院ID
        if (_strHopitalIdLookDeltail != "")
        {
            _strTemp += " AccountId_YY=" + _strHopitalIdLookDeltail + " And";
        }
        //医院名称
        string _strHospitalName = this.txtHospitalName.Text.ToString();
        if (_strHospitalName != "")
        {
            _strTemp += "  HospitalName like '%" + _strHospitalName + "%' and";
        }

        //医院编码
        string _strUserCode_YY = this.txtUserCode_YY.Text.ToString();
        if (_strUserCode_YY != "")
        {
            _strTemp += " UserCode_YY like '%" + _strUserCode_YY + "%' and";
        }
        //生产企业ID
        if (_strScLookDeltail != "")
        {
            _strTemp += " AccountId_SC=" + _strScLookDeltail + " And";
        }
        //生产企业名称
        string _strCompanyName_sc = this.txtCompanyName_sc.Text.ToString();
        if (_strCompanyName_sc != "")
        {
            _strTemp += " CompanyName_sc  like '%" + _strCompanyName_sc + "%' and";
        }
        //生产企业编码
        string _strUserCode_SC = this.txtUserCode_SC.Text.ToString();
        if (_strUserCode_SC != "")
        {
            _strTemp += " UserCode_SC like '%" + _strUserCode_SC + "%' and";
        }
        //地区
        //_strTemp += SysFun.AreaIdWhere(_AreaIdDeltail);

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }

        return _strTemp;
    }
    protected void btnPrint_Click(object sender, EventArgs e)  //打印
    {
        Db db = new Db();
        string StrSql="";
        System.Data.IDataReader dr;
        StrSql = "select HspId,CompanyName_TB,GoodsId,ProductName,GoodsName,MedicineModel,Outlookc,Factor,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,PurchasePrice,accountid_sc,UserCode_SC,CompanyName_SC,CompanyName_TB,UserCode_TB,HospitalName,accountid_ps,UserCode_PS,CompanyName_PS,BidPrice,PlanPurNum,convert(numeric(30,2),BidPrice*PlanPurNum) as PlanPrice from PurchaseTable where " + fctConditions();
        dr = db.DataReader(StrSql);
        this.GridView1.DataSource = dr;
        this.GridView1.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        if (GridView1.Rows.Count > 0)
        {
            GridView1.Visible = true;
            Response.Clear();
            Response.ContentType = "application/x-excel";
            Response.AddHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("明细") + ".xls\"");
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            sb.Append(@"<style> .text { mso-number-format:\@; } </style> ");
            sb.Append("<html><body>");
            GridView1.RenderControl(htmlWrite);
            sb.Append("</body></html>");
            Response.Write(stringWrite.ToString());
            Response.End();
            GridView1.DataSource = null;
            GridView1.Dispose();
        }
       
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    } 

}
